import Vue from 'vue'
import Router from 'vue-router'
import routes from './routers'
import iView from 'view-design'
import { getToken } from '@/libs/util'

Vue.use(Router)
const router = new Router({
  routes,
  mode: 'history'
})

router.beforeEach((to, from, next) => {
  iView.LoadingBar.start()
  const token = getToken()
  if (token) {
    next()
  } else {
    // eslint-disable-next-line no-unused-expressions
    '/login' === to.fullPath ? next() : next('/login')
  }
})

router.afterEach(() => {
  iView.LoadingBar.finish()
  // window.scrollTo(0, 0)
})

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
export default router
